package org.altervista.cp.microkernel.persistence.spi;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public interface Persister<T> {

	void delete(Connection c, T o) throws SQLException;

	void insert(Connection c, T o) throws SQLException;

	T select(Connection c, Object... id) throws SQLException;

	T[] selectAll(Connection c) throws SQLException;

	void update(Connection c, T o) throws SQLException;

	PersistersMap MAP = new PersistersMap();

	class PersistersMap {

		private PersistersMap() {
		}

		@SuppressWarnings("unchecked")
		public <T> Persister<T> get(Class<? extends T> c) {
			Persister<T> p = (Persister<T>) _map.get(c);
			if (p == null) {
				p = new DefaultPersister<T>(c);
				_map.put(c, p);
			}
			return p;
		}

		public <T> void put(Class<T> c, Persister<T> p) {
			_map.put(c, p);
		}

		private Map<Class<?>, Persister<?>> _map = Collections
				.synchronizedMap(new HashMap<Class<?>, Persister<?>>());

	}

}
